package com.zzxu.homeword;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.TextView;

public class SplashActivity extends AppCompatActivity {
    private TextView tvCountdown;
    private CountDownTimer countDownTimer;
    private boolean isSkipped = false;
    private static final int DURATION = 5000; // 5秒

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        tvCountdown = findViewById(R.id.tv_countdown);

        // 设置点击跳过
        tvCountdown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                skipToTarget();
            }
        });

        startCountdown();
    }

    // 启动倒计时
    private void startCountdown() {
        countDownTimer = new CountDownTimer(DURATION, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                long seconds = millisUntilFinished / 1000;
                tvCountdown.setText(seconds + "秒");
            }

            @Override
            public void onFinish() {
                if (!isSkipped) {
                    navigateToTarget();
                }
            }
        };
        countDownTimer.start();
    }

    // 跳过逻辑
    private void skipToTarget() {
        isSkipped = true;
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        navigateToTarget();
    }

    // 跳转到目标网址
    private void navigateToTarget() {
        String targetUrl = "http://121.250.190.208"; // 替换为目标网址
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(targetUrl));
        startActivity(intent);
        finish(); // 关闭启动页
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }
}